package com.changgou.service.goods.service.impl;


import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.changgou.goods.pojo.Brand;
import com.changgou.service.goods.dao.BrandMapper;
import com.changgou.service.goods.service.BrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {
    @Resource
    private BrandMapper brandMapper;


    /**
     * 品牌列表查询
     *
     * @return
     */
    @Override
    public List<Brand> findList() {
        return this.list();
    }

    /**
     * 根据id查询品牌信息
     *
     * @param id
     * @return
     */
    @Override
    public Brand findById(Integer id) {
        return this.getById(id);
    }

    /**
     * 品牌新增
     *
     * @param brand
     */
    @Override
    @Transactional
    public void add(Brand brand) {
        this.save(brand);
    }

    /**
     * 品牌修改
     *
     * @param brand
     */
    @Override
    @Transactional
    public void update(Brand brand) {
        this.updateById(brand);
    }

    /**
     * 根据id删除品牌信息
     *
     * @param id
     */
    @Override
    @Transactional
    public void delById(Integer id) {
        this.removeById(id);
    }

    /**
     * 品牌列表条件查询
     *
     * @param searchMap
     * @return
     */
    @Override
    public List<Brand> list(Map<String, Object> searchMap) {
        if (ObjectUtil.isNotNull(searchMap)) {
            LambdaQueryWrapper<Brand> lambdaQuery = Wrappers.<Brand>lambdaQuery()
                    .like(ObjectUtil.isNotNull(searchMap.get("name")) && ObjectUtil.isNotEmpty(searchMap.get("name")),
                            Brand::getName, searchMap.get("name"))
                    .or()
                    .eq(ObjectUtil.isNotNull(searchMap.get("name")) && ObjectUtil.isNotEmpty(searchMap.get("name")),
                            Brand::getLetter, searchMap.get("letter"));
            return this.list(lambdaQuery);
        }
        return ListUtil.empty();
    }

    @Override
    public Page<Brand> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        List<Brand> brands = this.brandMapper.selectList(null);
        Page<Brand> pageResult = (Page<Brand>) brands;
        return pageResult;
    }

    @Override
    public Page<Brand> findPage(Map<String, Object> searchMap, int page, int size) {
        //设置分页
        PageHelper.startPage(page, size);
        if (ObjectUtil.isNotNull(searchMap)) {
            LambdaQueryWrapper<Brand> lambdaQuery = Wrappers.<Brand>lambdaQuery()
                    .like(ObjectUtil.isNotNull(searchMap.get("name")) && ObjectUtil.isNotEmpty(searchMap.get("name")),
                            Brand::getName, searchMap.get("name"))
                    .or()
                    .eq(ObjectUtil.isNotNull(searchMap.get("name")) && ObjectUtil.isNotEmpty(searchMap.get("name")),
                            Brand::getLetter, searchMap.get("letter"));
            return (Page<Brand>) this.list(lambdaQuery);
        }
        return null;
    }

    /**
     * 根据分类名称查询品牌列表
     *
     * @param categoryName
     * @return
     */
    @Override
    public List<Map> findListByCategoryName(String categoryName) {
        return this.brandMapper.findListByCategoryName(categoryName);
    }
}
